/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.module.properties;

import java.awt.Font;
import java.util.HashMap;
import java.util.StringTokenizer;
import org.w3c.dom.NodeList;
import tal.drivechain.chain.ChainNode;

/**
 *
 * @author Main
 */
public class FontParser implements PropertyParser {
    int defaultSize = 12;
    int defaultStyle = Font.PLAIN;
    
    public String getPropertyType() {
        return "font";
    }

    public void setAttribures(HashMap<String, String> attributes, NodeList childeElements) throws IllegalArgumentException {
        String strSize = attributes.get("default-size");
        try {
            if (strSize!=null) defaultSize = Integer.parseInt(strSize);
        } catch (NumberFormatException n) {
            throw new IllegalArgumentException("Invalid default-size value: " + strSize);
        }
        String strStyle = attributes.get("default-style");
        if (strStyle!=null) defaultStyle = parseStyle(strStyle);
    }

    public Object parseProperty(String props) throws IllegalArgumentException {
        //syntax: font-name/size/style
        StringTokenizer fontProps = new StringTokenizer(props, "/");
        String fontname = null;
        int style = defaultStyle;
        int size = defaultSize;
        int count = fontProps.countTokens();
        for (int i=0; i<count; i++) {
            String fontProp = fontProps.nextToken();
            if (i==0) fontname = fontProp;
            else if (i==1) {
                try {
                    size = Integer.parseInt(fontProp);
                } catch (NumberFormatException n) {
                    throw new IllegalArgumentException("Invalid font size: " + fontProp);
                }
            } else if (i==2) {
                style = parseStyle(fontProp);
            }
        }
        return new Font(fontname, style, size);
    }

    private int parseStyle(String style) throws IllegalArgumentException {
        if (style.equals("bold")) return Font.BOLD;
        else if (style.equals("italic")) return Font.ITALIC;
        else if (style.equals("plain")) return Font.PLAIN;
        else throw new IllegalArgumentException("Unknown style name: " + style);
    }

    public String propToString(Object property, ChainNode context) {
        Font font = (Font)property;
        return font.getName() + "/" + font.getSize() + "/" + styleToConstant(font.getStyle());
    }

    private String styleToConstant(int style) throws IllegalArgumentException {
        switch (style) {
            case Font.BOLD:
                return "bold";
            case Font.ITALIC:
                return "italic";
            case Font.PLAIN:
                return "plain";
            default:
                throw new IllegalArgumentException("Unknown font style: " + style);
        }
    }

}
